﻿using System;
using System.Runtime.InteropServices;

namespace Golang.Ioc
{
    internal class Program
    {
        [DllImport(
            "Golang.Ioc.Interop.dll",
            EntryPoint = "Check",
            CallingConvention = CallingConvention.Cdecl
        )]
        public static extern bool Check(int i1, int i2);

        [DllImport(
            "Golang.Ioc.Interop.dll",
            EntryPoint = "GetSlogan",
            CallingConvention = CallingConvention.Cdecl
        )]
        [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CStringMarshaler))]
        public static extern string GetSlogan(
            [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CStringMarshaler))]
            string name
        );

        private static void Main(string[] args)
        {
            CheckDemo();
            StringDemo();
            StringTest();
            Console.ReadLine();
        }

        private static void CheckDemo()
        {
            Console.WriteLine($"Check(1,2):{Check(1, 2)}");
            Console.WriteLine($"Check(2,2):{Check(2, 2)}");
            Console.WriteLine($"Check(3,2):{Check(3, 2)}");
        }

        private static void StringDemo()
        {
            var r = GetSlogan("码农很忙");
            Console.WriteLine(r);
        }

        private static void StringTest()
        {
            var total = 0;
            while (true)
            {
                for (int i = 0; i < 1000; i++)
                {
                    total++;
                    GetSlogan("码农很忙");
                }
                Console.WriteLine($"调用 {total} 次");
                Console.ReadLine();
            }
        }
    }
}